package org.alexdalton.jira;

import java.util.ArrayList;

import org.alexdalton.jira.model.JiraServer;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

public class JiraApp extends Application implements OnSharedPreferenceChangeListener {


    private static JiraApp app;

    public boolean allowAllSSL;

    public JiraConn conn;

    private JiraServersDB db;

    private void loadPreferences(SharedPreferences prefs, boolean startup) {

        allowAllSSL = prefs.getBoolean("allowallssl", false);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        loadPreferences(prefs, true);
        prefs.registerOnSharedPreferenceChangeListener(this);
        db = new JiraServersDB(this);
        servers = db.getServerList();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    public static JiraApp get() {
        return app;
    }

    public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        loadPreferences(arg0, false);
    }

    public JiraConn getCurrentConnection() {
        return conn;
    }

    public void setCurrentConnection(JiraConn conn) {
        this.conn = conn;
    }

    ArrayList<JiraServer> servers = new ArrayList<JiraServer>();
    
    public void addServer(String name, String url, String user, String pass) {
        // TODO Auto-generated method stub
        db.addServer(new JiraServer(name, url, user, pass));
        servers = db.getServerList();
    }

    public ArrayList<JiraServer> getServerList() {
        return servers;
    }

    public JiraServer getServerFromName(String name) {
        for (int i = 0; i < servers.size(); i++) {
            if (servers.get(i).getName().equals(name)) {
                return servers.get(i);
            }
        }
        return null;
    }
}
